<?php

/**
 * FactureFournisseur form base class.
 *
 * @method FactureFournisseur getObject() Returns the current form's model object
 *
 * @package    SdCommerce
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseFactureFournisseurForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                     => new sfWidgetFormInputHidden(),
      'contact_id'             => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Contact'), 'add_empty' => true)),
      'date'                   => new sfWidgetFormDate(),
      'reference'              => new sfWidgetFormInputText(),
      'observation'            => new sfWidgetFormInputText(),
      'unite_duree_validate'   => new sfWidgetFormInputText(),
      'taux_remise'            => new sfWidgetFormInputText(),
      'statut'                 => new sfWidgetFormChoice(array('choices' => array('Reglée' => 'Reglée', 'Non Reglée' => 'Non Reglée', 'Partiellement Reglée' => 'Partiellement Reglée'))),
      'total_ht'               => new sfWidgetFormInputText(),
      'total_remise'           => new sfWidgetFormInputText(),
      'total_tva'              => new sfWidgetFormInputText(),
      'total_ttc'              => new sfWidgetFormInputText(),
      'rest'                   => new sfWidgetFormInputText(),
      'timbre'                 => new sfWidgetFormInputCheckbox(),
      'date_validite'          => new sfWidgetFormDate(),
      'duree_validite'         => new sfWidgetFormInputText(),
      'cloture'                => new sfWidgetFormInputCheckbox(),
      'domaine'                => new sfWidgetFormInputCheckbox(),
      'mode_paiement_id'       => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('ModeReglement'), 'add_empty' => true)),
      'fournisseur_id'         => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Fournisseur'), 'add_empty' => true)),
      'departement_id'         => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Departement'), 'add_empty' => true)),
      'representant_id'        => new sfWidgetFormInputText(),
      'taux_escompte'          => new sfWidgetFormInputText(),
      'num_attestation_timbre' => new sfWidgetFormInputText(),
      'confirme'               => new sfWidgetFormInputCheckbox(),
      'valider'                => new sfWidgetFormInputCheckbox(),
      'affaire_id'             => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Affaire'), 'add_empty' => true)),
      'brouillon'              => new sfWidgetFormInputCheckbox(),
      'date_changement_etat'   => new sfWidgetFormDate(),
      'avance_sur_impot'       => new sfWidgetFormInputText(),
      'total_fodec'            => new sfWidgetFormInputText(),
      'journal_caisse'         => new sfWidgetFormInputCheckbox(),
      'is_bon_livraison'       => new sfWidgetFormInputCheckbox(),
      'created_at'             => new sfWidgetFormDateTime(),
      'updated_at'             => new sfWidgetFormDateTime(),
      'deleted_at'             => new sfWidgetFormDateTime(),
      'factures_list'          => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'FactureAvoirFournisseur')),
    ));

    $this->setValidators(array(
      'id'                     => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'contact_id'             => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Contact'), 'required' => false)),
      'date'                   => new sfValidatorDate(),
      'reference'              => new sfValidatorString(array('max_length' => 20)),
      'observation'            => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'unite_duree_validate'   => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'taux_remise'            => new sfValidatorNumber(array('required' => false)),
      'statut'                 => new sfValidatorChoice(array('choices' => array(0 => 'Reglée', 1 => 'Non Reglée', 2 => 'Partiellement Reglée'), 'required' => false)),
      'total_ht'               => new sfValidatorNumber(array('required' => false)),
      'total_remise'           => new sfValidatorNumber(array('required' => false)),
      'total_tva'              => new sfValidatorNumber(array('required' => false)),
      'total_ttc'              => new sfValidatorNumber(array('required' => false)),
      'rest'                   => new sfValidatorNumber(array('required' => false)),
      'timbre'                 => new sfValidatorBoolean(array('required' => false)),
      'date_validite'          => new sfValidatorDate(array('required' => false)),
      'duree_validite'         => new sfValidatorInteger(array('required' => false)),
      'cloture'                => new sfValidatorBoolean(array('required' => false)),
      'domaine'                => new sfValidatorBoolean(),
      'mode_paiement_id'       => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('ModeReglement'), 'required' => false)),
      'fournisseur_id'         => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Fournisseur'), 'required' => false)),
      'departement_id'         => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Departement'), 'required' => false)),
      'representant_id'        => new sfValidatorInteger(array('required' => false)),
      'taux_escompte'          => new sfValidatorNumber(array('required' => false)),
      'num_attestation_timbre' => new sfValidatorInteger(array('required' => false)),
      'confirme'               => new sfValidatorBoolean(array('required' => false)),
      'valider'                => new sfValidatorBoolean(array('required' => false)),
      'affaire_id'             => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Affaire'), 'required' => false)),
      'brouillon'              => new sfValidatorBoolean(array('required' => false)),
      'date_changement_etat'   => new sfValidatorDate(array('required' => false)),
      'avance_sur_impot'       => new sfValidatorNumber(array('required' => false)),
      'total_fodec'            => new sfValidatorNumber(array('required' => false)),
      'journal_caisse'         => new sfValidatorBoolean(array('required' => false)),
      'is_bon_livraison'       => new sfValidatorBoolean(array('required' => false)),
      'created_at'             => new sfValidatorDateTime(),
      'updated_at'             => new sfValidatorDateTime(),
      'deleted_at'             => new sfValidatorDateTime(array('required' => false)),
      'factures_list'          => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'FactureAvoirFournisseur', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('facture_fournisseur[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'FactureFournisseur';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['factures_list']))
    {
      $this->setDefault('factures_list', $this->object->factures->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->savefacturesList($con);

    parent::doSave($con);
  }

  public function savefacturesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['factures_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->factures->getPrimaryKeys();
    $values = $this->getValue('factures_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('factures', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('factures', array_values($link));
    }
  }

}
